import { useEffect, useRef, useState } from "react";
function debounce(func, wait) {
  let timeout;

  return function () {
    const context = this,
      args = arguments;

    clearTimeout(timeout);
    timeout = setTimeout(() => func.apply(context, args), wait);
  };
}
const useScroll = () => {
  const [direction, setDirection] = useState("");
  const lastScrollTop = useRef(null);
  //   var lastScrollTop = 0,
  //     ticking = false;
  // direction;
console.log('directiondirection',direction)
  useEffect(() => {
    const handle = debounce((event) => {
      
      let st = window.pageYOffset || document.documentElement.scrollTop;
      // console.log("debounce",st,lastScrollTop.current);
      if (st > lastScrollTop.current) {
        setDirection('down')
        // console.log("debounce down");
      } else {
        setDirection('up')
        // console.log("debounce up");
      }

      lastScrollTop.current = st <= 0 ? 0 : st;
    }, 300);
    // handle()
    window.addEventListener("scroll", handle, { passive: true });
    return () => {
      window.removeEventListener("scroll", handle);
    };
  }, []);
  return { direction };
};
export default useScroll;
